﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class PhysicalCountReportList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15291;
        protected GridView gvPhysicalCountReport;
        protected GridView gvPhysicalCountReportShowLot;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnShowLot;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblPhysicalCountReportList;
        protected Label lblTotalInvQty;
        protected Label lblTotalInvQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalInvQty;
        protected UpdatePanel up1;

        public void BindData2gvPhysicalCountReport(PhysicalCountRptQueryEntity physicalCountRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(physicalCountRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetPhysicalCountReportList(physicalCountRptQueryEntity);
            if (physicalCountRptQueryEntity.ShowLot == "Y")
            {
                this.gvPhysicalCountReportShowLot.Visible = true;
                this.gvPhysicalCountReport.Visible = false;
                base.BindData2GridView(this.gvPhysicalCountReportShowLot, ds);
            }
            else
            {
                this.gvPhysicalCountReport.Visible = true;
                this.gvPhysicalCountReportShowLot.Visible = false;
                base.BindData2GridView(this.gvPhysicalCountReport, ds);
            }
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PhysicalCountRptQueryEntity physicalCountRptQueryEntity = this.GetPhysicalCountRptQueryEntity();
            physicalCountRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = physicalCountRptQueryEntity.CurrentPage;
            physicalCountRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetPhysicalCountReportList(physicalCountRptQueryEntity))
                {
                    if (physicalCountRptQueryEntity.ShowLot == "Y")
                    {
                        base.Export2Excel("PhysicalCountRpt.xls", "15291", this.gvPhysicalCountReportShowLot, ds);
                    }
                    else
                    {
                        base.Export2Excel("PhysicalCountRpt.xls", "15291", this.gvPhysicalCountReport, ds);
                    }
                }
            }
            finally
            {
                physicalCountRptQueryEntity.IsGetAll = false;
                physicalCountRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetPhysicalCountRptQueryEntity(physicalCountRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PhysicalCountRptQueryEntity physicalCountRptQueryEntity = this.GetPhysicalCountRptQueryEntity();
            physicalCountRptQueryEntity.CurrentPage = "0";
            this.BindData2gvPhysicalCountReport(physicalCountRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PhysicalCountRptQueryEntity physicalCountRptQueryEntity = this.GetPhysicalCountRptQueryEntity();
            physicalCountRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2gvPhysicalCountReport(physicalCountRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            PhysicalCountRptQueryEntity physicalCountRptQueryEntity = this.GetPhysicalCountRptQueryEntity();
            physicalCountRptQueryEntity.OwnerID = "";
            physicalCountRptQueryEntity.SkuID = "";
            physicalCountRptQueryEntity.SkuClassID = "";
            physicalCountRptQueryEntity.StartLoc = "";
            physicalCountRptQueryEntity.ExternalLot = "";
            physicalCountRptQueryEntity.VendorID = "";
            physicalCountRptQueryEntity.Lot = "";
            physicalCountRptQueryEntity.LPN = "";
            physicalCountRptQueryEntity.SkuDescr = "";
            physicalCountRptQueryEntity.ZoneID = "";
            this.BindData2gvPhysicalCountReport(physicalCountRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PhysicalCountRptQueryEntity physicalCountRptQueryEntity = this.GetPhysicalCountRptQueryEntity();
            if (int.Parse(physicalCountRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                physicalCountRptQueryEntity.CurrentPage = (int.Parse(physicalCountRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2gvPhysicalCountReport(physicalCountRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PhysicalCountRptQueryEntity physicalCountRptQueryEntity = this.GetPhysicalCountRptQueryEntity();
            if (int.Parse(physicalCountRptQueryEntity.CurrentPage) > 0)
            {
                physicalCountRptQueryEntity.CurrentPage = (int.Parse(physicalCountRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2gvPhysicalCountReport(physicalCountRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            PhysicalCountRptQueryEntity physicalCountRptQueryEntity = this.GetPhysicalCountRptQueryEntity();
            this.BindData2gvPhysicalCountReport(physicalCountRptQueryEntity);
        }

        private PhysicalCountRptQueryEntity GetPhysicalCountRptQueryEntity()
        {
            return (base.PageQueryEntitySession as PhysicalCountRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PhysicalCountRptQueryEntity physicalCountRptQueryEntity = this.GetPhysicalCountRptQueryEntity();
                physicalCountRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2gvPhysicalCountReport(physicalCountRptQueryEntity);
            }
        }

        protected void gvPhysicalCountDiffRptShowLot_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15291", this.gvPhysicalCountReportShowLot);
        }

        protected void gvPhysicalCountReport_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15291", this.gvPhysicalCountReport);
        }

        protected void gvPhysicalCountReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblInvQty")) != null)
                {
                    this.TotalInvQty += decimal.Parse((((Label) e.Row.FindControl("lblInvQty")).Text != "") ? ((Label) e.Row.FindControl("lblInvQty")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalInvQtyText.Text = base.GetPreferenceDigit(this.TotalInvQty);
            }
        }

        protected void gvPhysicalCountReport_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PhysicalCountRptQueryEntity physicalCountRptQueryEntity = this.GetPhysicalCountRptQueryEntity();
                physicalCountRptQueryEntity.SortField = e.SortExpression;
                if (physicalCountRptQueryEntity.SortDirection == "ASC")
                {
                    physicalCountRptQueryEntity.SortDirection = "DESC";
                }
                else if (physicalCountRptQueryEntity.SortDirection == "DESC")
                {
                    physicalCountRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2gvPhysicalCountReport(physicalCountRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            PhysicalCountRptQueryEntity physicalCountRptQueryEntity = new PhysicalCountRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    physicalCountRptQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    physicalCountRptQueryEntity.SkuClassID = this.PreviousPage.SkuClassID;
                    physicalCountRptQueryEntity.SkuID = this.PreviousPage.SkuID;
                    physicalCountRptQueryEntity.StartLoc = this.PreviousPage.StartLoc;
                    physicalCountRptQueryEntity.ShowLoc = this.PreviousPage.ShowLoc;
                    physicalCountRptQueryEntity.ExternalLot = this.PreviousPage.ExternalLot;
                    physicalCountRptQueryEntity.VendorID = this.PreviousPage.VendorID;
                    physicalCountRptQueryEntity.Lot = this.PreviousPage.Lot;
                    physicalCountRptQueryEntity.LPN = this.PreviousPage.Lpn;
                    physicalCountRptQueryEntity.SkuDescr = this.PreviousPage.SkuDescr;
                    physicalCountRptQueryEntity.ShowLot = this.PreviousPage.ShowLot;
                    physicalCountRptQueryEntity.ZoneID = this.PreviousPage.ZoneID;
                    if (physicalCountRptQueryEntity.ShowLot == "Y")
                    {
                        this.gvPhysicalCountReportShowLot.Visible = true;
                        this.gvPhysicalCountReport.Visible = false;
                    }
                }
                else
                {
                    physicalCountRptQueryEntity.OwnerID = "";
                    physicalCountRptQueryEntity.SkuClassID = "";
                    physicalCountRptQueryEntity.SkuID = "";
                    physicalCountRptQueryEntity.StartLoc = "";
                    physicalCountRptQueryEntity.ShowLoc = "";
                    physicalCountRptQueryEntity.ShowLot = "";
                    physicalCountRptQueryEntity.ExternalLot = "";
                    physicalCountRptQueryEntity.VendorID = "";
                    physicalCountRptQueryEntity.Lot = "";
                    physicalCountRptQueryEntity.LPN = "";
                    physicalCountRptQueryEntity.SkuDescr = "";
                    this.hdnShowLot.Value = "";
                    physicalCountRptQueryEntity.ZoneID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            physicalCountRptQueryEntity.IsGetAll = false;
            physicalCountRptQueryEntity.CurrentPage = "0";
            if (physicalCountRptQueryEntity.ShowLot == "Y")
            {
                physicalCountRptQueryEntity.PageSize = this.gvPhysicalCountReportShowLot.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvPhysicalCountReportShowLot.PageSize;
            }
            else
            {
                physicalCountRptQueryEntity.PageSize = this.gvPhysicalCountReport.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvPhysicalCountReport.PageSize;
            }
            physicalCountRptQueryEntity.SortField = "L.LOC,A.SKU_ID";
            physicalCountRptQueryEntity.SortDirection = "ASC";
            this.SetPhysicalCountRptQueryEntity(physicalCountRptQueryEntity);
            this.BindData2gvPhysicalCountReport(physicalCountRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvPhysicalCountReport);
                base.InitAnalyticsReportGridview(this.gvPhysicalCountReportShowLot);
                this.LoadData();
            }
        }

        public void SetPhysicalCountRptQueryEntity(PhysicalCountRptQueryEntity physicalCountRptQueryEntity)
        {
            base.PageQueryEntitySession = physicalCountRptQueryEntity;
        }

        public PhysicalCountRptQuery PreviousPage
        {
            get
            {
                return (PhysicalCountRptQuery) base.PreviousPage;
            }
        }
    }
}

